package com.liangtian.mdm.dm.checkRule.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.dm.checkRule.model.CheckRule;
import com.liangtian.mdm.dm.checkRule.service.CheckRuleService;
import com.liangtian.mdm.dm.common.pojo.ResponseMsg;
import com.liangtian.mdm.dm.common.util.RequestUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/checkRuleController")
public class CheckRuleController extends BaseController {

    @Autowired
    CheckRuleService checkRuleService;

    /**
     * 查询所有
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryCheckRule", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryCheckRule(HttpServletRequest request, CheckRule checkRule) {
        Map paramMap = RequestUtils.paramToMap(request, CheckRule.class, false);
        PageInfo pageInfo = checkRuleService.queryAllCheckByCondition(paramMap);
        List l = pageInfo.getList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", l);// JSONArray
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping(value = "/queryAllRulesByType", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String queryAllRulesByType(HttpServletRequest request) {
        String rst = null;
        try {
            Map paramMap = RequestUtils.paramToMap(request, CheckRule.class, false);
            List<CheckRule> checkRuleMapList = checkRuleService.queryAllRulesByType(paramMap);
            rst = JSON.toJSONString(checkRuleMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rst;
    }

    /**
     * 新增
     *
     * @param checkRule
     * @return
     */
    @RequestMapping(value = "/insertRule", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insertRule(CheckRule checkRule) {

        int count = checkRuleService.insertRule(checkRule);
        ResultObject resultObject = new ResultObject(count, "新增成功", "新增失败");
        return resultObject;
    }

    /**
     * 校验名称
     *
     * @param checkRule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkRuleName", method = RequestMethod.POST)
    public JSONObject checkRuleName(CheckRule checkRule) {
        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            int count = checkRuleService.checkRuleName(checkRule);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;
    }

    /**
     * 编辑
     *
     * @param checkRule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkRuleUpdate", method = RequestMethod.POST)
    public JSONObject checkRuleUpdate(CheckRule checkRule) {
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            checkRuleService.updateRule(checkRule);
            rMsg.setRspCode(0);
        } catch (Exception e) {
            e.printStackTrace();
            rMsg.setRspCode(-1);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rMsg", rMsg);
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping(value = "/queryOne", method = RequestMethod.POST)
    public JSONObject queryOne(CheckRule checkRule) {
        JSONObject jsonObject = new JSONObject();
        CheckRule rule = null;
        try {
            rule = checkRuleService.getById(checkRule.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rule", rule);
        return jsonObject;
    }

}
